package com.hu.gulimallproduct.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hu.gulimallcommon.utils.PageUtils;
import com.hu.gulimallcommon.utils.R;
import com.hu.gulimallproduct.project.entity.PmsBrandEntity;
import com.hu.gulimallproduct.project.entity.PmsCategoryBrandRelationEntity;
import com.hu.gulimallproduct.project.service.PmsCategoryBrandRelationService;
import com.hu.gulimallproduct.project.vo.BrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author hbt
 * @email 2188748205@qq.com
 * @date 2024-05-20 12:32:11
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class PmsCategoryBrandRelationController {
    @Autowired
    private PmsCategoryBrandRelationService pmsCategoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsCategoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 分类下的品牌列表
     * 1、controller：处理请求，接受和检验数据
     * 2、service接受controler传递参数，进行业务处理
     * 3、controller接受service处理玩的数据，封装成页面响应数据
     */
    @RequestMapping("brands/list")
    public R relationBrandsList( @RequestParam(value = "catId") Long catId){
        List<PmsBrandEntity> list = pmsCategoryBrandRelationService.relationBrandsList(catId);
        List<BrandVO> collect = list.stream().map(item -> {
            BrandVO vo = new BrandVO();
            vo.setBrandId(item.getBrandId());
            vo.setBrandName(item.getName());
            return vo;
        }).collect(Collectors.toList());

        return R.ok().put("data", collect);
    }

    /**
     * 获取当前品牌关联的所有分类
     */
    @RequestMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId){

        List<PmsCategoryBrandRelationEntity> list = pmsCategoryBrandRelationService.list(new QueryWrapper<PmsCategoryBrandRelationEntity>().eq("brand_id",brandId));

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PmsCategoryBrandRelationEntity pmsCategoryBrandRelation = pmsCategoryBrandRelationService.getById(id);

        return R.ok().put("pmsCategoryBrandRelation", pmsCategoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation){
		pmsCategoryBrandRelationService.saveDetil(pmsCategoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation){
		pmsCategoryBrandRelationService.updateById(pmsCategoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		pmsCategoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
